const { menuModel, menuAndJobModel } = require("../model");

//添加菜单
exports.addMenu = async (req, res) => {
  const { mName, mValue } = req.body;
  //查询是否在数据库存在
  let result = await menuModel.findOne({ mName });
  let result2 = await menuModel.findOne({ mValue });
  if (result || result2) {
    return res.status(400).json({
      code: 400,
      message: "菜单名或者值已存在",
      data: null,
    });
  }
  //不存在则添加
  let dbBack = new menuModel({
    mName,
    mValue,
  });
  try {
    await dbBack.save();
    res.status(200).json({
      code: 200,
      message: "添加成功",
      data: null,
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: "服务器出现问题",
      data: error,
    });
  }
};
//查询菜单
exports.getMenu = async (req, res) => {
  const { pageNo = 1, pageSize = 5 } = req.query;
  console.log(pageNo, pageSize);
  try {
    let result = await menuModel
      .find()
      .skip((pageNo - 1) * pageSize)
      .limit(pageSize);
    let total = (await menuModel.find()).length;
    res.status(200).json({
      code: 200,
      message: "查询成功",
      data: result,
      total,
    });
  } catch (error) {
    res.status(500).json({
      code: 500,
      message: "服务器出现问题",
      data: error,
    });
  }
};
//查询菜单详情
exports.getMenuInfo = async (req, res) => {
  const { id } = req.query;
  if (!id) {
    return res.status(400).json({
      code: 400,
      message: "菜单id不能为空",
      data: null,
    });
  }
  try {
    let result = await menuModel.findById(id);
    res.status(200).json({
      code: 200,
      message: "获取成功",
      data: result,
    });
  } catch (error) {
    console.log(error);
    res.status(500).json({
      code: 500,
      message: "获取失败",
      data: error,
    });
  }
};
//修改菜单
exports.updateMenu = async (req, res) => {
  const { id } = req.query;
  const { mName, mValue } = req.body;
  if (!id) {
    return res.status(400).json({
      code: 400,
      message: "菜单id不能为空",
      data: null,
    });
  }
  //查询修改的字段是否在数据库里
  if (!mName || !mValue) {
    return res.status(400).json({
      code: 400,
      message: "修改的字段不能为空",
      data: null,
    })
  }
  let result = await menuModel.findById(id)
 if(result.mName==mName||result.mValue==mValue) {
    return res.status(400).json({
      code:400,
      message:'修改的字段没有变化',
      data:null
    })
 }
 let result1 = await menuModel.find({mName})
 let result2 = await menuModel.find({mValue})
 if(result1.length>=1||result2>=1) {
  return res.status(400).json({
    code:400,
    message:'菜单名或者值已存在',
    data:null
  })
 }
  try {
    await menuModel.findByIdAndUpdate(id, {
      mName,
      mValue,
    });
    res.status(200).json({
        code:200,
        message:'修改成功',
        data:null  
    })
  } catch (error) {
    res.status(500).json({
        code:500,
        message:'修改失败',
        data:null  
    })
  }
};
//删除菜单
exports.deleteMenu = async (req,res)=>{
    const {id} = req.query
    if (!id) {
        return res.status(400).json({
          code: 400,
          message: "菜单id不能为空",
          data: null,
        });
      }
      let result = await menuAndJobModel.find({mid:id})
      if(result.length>=1) {
        return res.status(400).json({
          code:400,
          message:'该菜单下存在职位，不能删除',
          data:null
        })
      }
   
      try {
        await menuModel.findByIdAndDelete(id);
        res.status(200).json({
          code:200,
          message:'删除成功',
          data:null
        })
      } catch (error) {
        res.status(500).json({
            code:500,
            message:'删除失败',
            data:null
          })
      }
}
